using System;
using System.Collections.Generic;
using System.Text;

using OR.Lazarus.Business.Entities.Config;
using OR.Reuse.Reflection;

namespace OR.Lazarus.Business
{
	public class CompilerEngineFactory
	{
		public CompilerEngineFactory()
		{
		}

		public ICompilerEngine CreateEngine( string pConfigFilename )
		{
			ICompilerEngine vReturn = null;

			ConfigDetail vConfig = new ConfigDetail();

			vConfig.LoadXmlFile( pConfigFilename );

			vReturn = this.CreateEngine( vConfig );

			return ( vReturn );
		}

		public ICompilerEngine CreateEngine( ConfigDetail pConfig )
		{
			ICompilerEngine vReturn = null;

			pConfig.Validate();

			string ClassAssemblyName = "{0},{1}";

			string vSourceClassName = String.Format( ClassAssemblyName, pConfig.EngineTemplates[ TemplateTypes.Source ].Class, pConfig.EngineTemplates[ TemplateTypes.Source ].Assembly );
			string vCompiledClassName = String.Format( ClassAssemblyName, pConfig.EngineTemplates[ TemplateTypes.Compiled ].Class, pConfig.EngineTemplates[ TemplateTypes.Compiled ].Assembly );
			string vValidatorClassName = String.Format( ClassAssemblyName, pConfig.EngineTemplates[ TemplateTypes.Validator ].Class, pConfig.EngineTemplates[ TemplateTypes.Validator ].Assembly );
			string vCompilerClassName = String.Format( ClassAssemblyName, pConfig.EngineTemplates[ TemplateTypes.Compiler ].Class, pConfig.EngineTemplates[ TemplateTypes.Compiler ].Assembly );
			string vGeneratorClassName = String.Format( ClassAssemblyName, pConfig.EngineTemplates[ TemplateTypes.Generator ].Class, pConfig.EngineTemplates[ TemplateTypes.Generator ].Assembly );

			string[] vTemplateClasses = null;
			string vClassName = null;

			vTemplateClasses  = new string[] { vSourceClassName, vCompiledClassName, vValidatorClassName, vCompilerClassName, vGeneratorClassName };
			vClassName = "OR.Lazarus.Business.CompilerEngine`5,OR.Lazarus.BackEnd";

			vReturn = ( ICompilerEngine )AssemblyUtility.GetInstance( vClassName, null, vTemplateClasses );

			return ( vReturn );
		}
	}
}
